package com.mmf.ancientcostume.adapter.goods;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.mmf.ancientcostume.R;
import com.mmf.ancientcostume.activity.ImagePreviewActivity;
import com.mmf.ancientcostume.adapter.home.ReleaseInfoImageAdapter;
import com.mmf.ancientcostume.base.adapter.BaseRecyclerAdapter;
import com.mmf.ancientcostume.common.utils.DipUtil;
import com.mmf.ancientcostume.common.utils.service.SecretConstant;
import com.mmf.ancientcostume.model.GoodsImg;
import com.mmf.ancientcostume.model.StaticData;
import com.mmf.ancientcostume.widget.RecyclerTransformation;
import com.mmf.ancientcostume.widget.ReleaseAdapterInterface;
import com.mmf.ancientcostume.widget.SpaceItemDecoration;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by MMF
 * date 2017/10/23
 * Description:评论列表图片适配器
 */
public class CommentImageAdapter extends BaseRecyclerAdapter<GoodsImg> {
    private int maxNum = 4;
    Picasso picasso;
    private int width;
    private ReleaseAdapterInterface releaseAdapterInterface;

    public CommentImageAdapter(Context context, int lineCount) {
        super(context);
        picasso = Picasso.with(context);
        width = (DipUtil.getWidth(context) - DipUtil.dip2px(context, 16 + lineCount)) / lineCount;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_release_info, parent, false);
        ViewHolder mHolder = new ViewHolder(view);
        return mHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.ivDel.setVisibility(View.GONE);
        //设置图片显示的宽高
        ViewGroup.MarginLayoutParams margin9 = new ViewGroup.MarginLayoutParams(
                viewHolder.ivRelease.getLayoutParams());
        RelativeLayout.LayoutParams layoutParams9 = new RelativeLayout.LayoutParams(margin9);
        layoutParams9.height = width;//设置图片的高度
        layoutParams9.width = width; //设置图片的宽度
        viewHolder.ivRelease.setLayoutParams(layoutParams9);
        final GoodsImg item = itemList.get(position);
        if (!TextUtils.isEmpty(item.getImgName())) {
            picasso.load(SecretConstant.LOAD_IMG + item.getImgName())
                    .noFade()
                    .placeholder(R.drawable.pictures_no)
                    .transform(new RecyclerTransformation(width))
                    .into(viewHolder.ivRelease);
        } else {
            viewHolder.ivRelease.setImageResource(R.drawable.pictures_no);
        }
        //点击图片进入图片预览
        viewHolder.ivRelease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                releaseAdapterInterface.jump(position, itemList);
            }
        });
        viewHolder.ivDel.setVisibility(View.GONE);
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_release)
        ImageView ivRelease;
        @BindView(R.id.iv_del)
        ImageView ivDel;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

    }

    public void setReleaseAdapterInterface(ReleaseAdapterInterface releaseAdapterInterface) {
        this.releaseAdapterInterface = releaseAdapterInterface;
    }
}
